package com.lebang.controller.sys;

import com.lebang.aop.ConfigRecord;
import com.lebang.common.Login;
import com.lebang.domain.LuckDrawSetting;
import com.lebang.domain.PointAdvertisementSetting;
import com.lebang.entity.TaskRewards;
import com.lebang.model.Whether;
import com.lebang.service.AdSettingService;
import com.lebang.service.LuckDrawService;
import com.lebang.service.LuckDrawSettingService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @Author: hwq
 * @Date: 2019/11/26 16:16
 * @Version 1.0
 */
@Api(tags = "抽奖活动")
@RequestMapping("/sys/luckdraw")
@Controller
public class SysLuckDrawController {
    @Autowired
    private AdSettingService adSettingService;
    @Autowired
    private LuckDrawSettingService settingService;

    @Autowired
    private LuckDrawService luckDrawService;

    @GetMapping("/list")
    @Login
    public String list(Integer id, Model model) {
        return "/luck/list";
    }

    @GetMapping("/add")
    @Login
    public String add(Integer id, Model model) {
        PointAdvertisementSetting setting;
        if (id != null) {
            setting = adSettingService.selectById(id);
        } else {
            setting = new PointAdvertisementSetting();
            setting.setEnable(Whether.YES.getDbIndex());
            setting.setCategory(1);
        }
        model.addAttribute("item", setting);
        return "/luck/add";
    }

    @PostMapping("/edit")
    @Login
    @ResponseBody
    @ConfigRecord
    public JsonData edit(PointAdvertisementSetting sort) {
        if (sort.getId() != null) {
            sort.setModifyTime(LocalDateTime.now());
            adSettingService.updateById(sort);
        } else {
            sort.setCreateTime(LocalDateTime.now());
            sort.setHasDeleted(Whether.NO.getDbIndex());
            adSettingService.insertModel(sort);
        }
        return Callback.success("success", "修改成功");
    }

    @Login
    @PostMapping("/enable")
    @ResponseBody
    @ConfigRecord
    public JsonData enable(@RequestBody PointAdvertisementSetting sort) {
        adSettingService.updateById(sort);
        return Callback.success("success", "操作成功");
    }


    @PostMapping("/clean")
    @Login
    @ResponseBody
    public JsonData clean(HttpServletRequest request) {
        luckDrawService.clean();
        return Callback.success("success", "清除缓存成功");
    }

    @GetMapping("/draw/list")
    @Login
    public String drawlist(Integer id, Model model) {
        return "/luck/setting";
    }

    @GetMapping("/draw/add")
    @Login
    public String drawadd(Integer id, Model model) {
        LuckDrawSetting setting;
        if (id != null) {
            setting = settingService.selectById(id);
        } else {
            setting = new LuckDrawSetting();
        }
        model.addAttribute("item", setting);
        return "/luck/setting-add";
    }

    @PostMapping("/draw/edit")
    @ResponseBody
    @Login
    @ConfigRecord
    public JsonData drawedit(LuckDrawSetting sort) {
        if (sort.getId() != null) {
            sort.setModifyTime(LocalDateTime.now());
            settingService.updateById(sort);
        } else {
            sort.setCreateTime(LocalDateTime.now());
            sort.setHasDeleted(Whether.NO.getDbIndex());
            settingService.insertModel(sort);
        }
        return Callback.success("success", "修改成功");
    }
}

